package com.jama.carouselviewexample.slice;

import com.jama.carouselview.views.BaseListContainer;
import com.jama.carouselview.views.CarouselScrollListener;
import com.jama.carouselview.views.WeakHandler;
import com.jama.carouselviewexample.ResourceTable;
import com.jama.carouselviewexample.adapter.CenterProvider;
import com.jama.carouselviewexample.bean.CenterBean;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.window.service.DisplayManager;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * CenteredCarouseActivitySlice
 *
 * @since 2021-05-24
 */
public class CenteredCarouseSlice extends AbilitySlice {
    private static final HiLogLabel LOG_LABEL = new HiLogLabel(0, 0, "CenteredCarouseActivitySlice");
    private static final int NUM_TIME = 2000;
    private static final int NUM_3 = 3;
    private Image image1;
    private Image image2;
    private Image image3;
    private Text textView;
    private Text textView1;
    private Text textView2;
    private BaseListContainer listContainer;
    private List<Image> imageList;
    private List<CenterBean> centerBeans;
    private int pageSliderPos = -1;
    private CenterProvider provider;
    private int num = 1;
    private WeakHandler handler;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            catchNum(num);

            num++;
            handler.postDelayed(runnable, NUM_TIME);
        }
    };

    private void catchNum(int index) {
        int height = listContainer.getHeight();
        int width = listContainer.getWidth();
        switch (index) {
            case 0:
                listContainer.scrollToCenter(index);
                changeSelect(1);
                debug("scrollToCenter 0" + index);
                break;
            case 1:
                listContainer.scrollTo(width / 2, height / 2);
                listContainer.scrollToCenter(index);
                changeSelect(2);
                debug("scrollToCenter 1" + index);
                break;
            case 2:
                listContainer.scrollToCenter(index);
                changeSelect(NUM_3);
                debug("scrollToCenter 2" + index);
                num = -1;
                break;
            default:
                return;
        }

    }

    /**
     * onStart
     *
     * @param intent intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_centered_carouse);
        initView();

    }

    /**
     * initView
     */
    private void initView() {
        listContainer = (BaseListContainer) findComponentById(ResourceTable.Id_iv_list);
        image1 = (Image) findComponentById(ResourceTable.Id_iv_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_iv_image2);
        image3 = (Image) findComponentById(ResourceTable.Id_iv_image3);
        textView = (Text) findComponentById(ResourceTable.Id_textView3);
        textView1 = (Text) findComponentById(ResourceTable.Id_textview);
        textView2 = (Text) findComponentById(ResourceTable.Id_textView4);

        textView1.setFont(Font.DEFAULT);
        textView2.setFont(Font.DEFAULT);
        textView.setFont(Font.DEFAULT_BOLD);
        centerBeans = createcenterBean();
        imageList = createImages();
        provider = new CenterProvider(centerBeans, this);
        listContainer.setOrientation(Component.HORIZONTAL);
        listContainer.setItemProvider(provider);

        listContainer.setCarouselScrollListener(new CarouselScrollListener() {
            @Override
            public void onScrollStateChanged(ListContainer container, int newState, int position) {
                changeSelect(position);
            }

            @Override
            public void onScrolled(ListContainer container, int dx, int dy) {
            }
        });
        EventRunner eventRunner = EventRunner.getMainEventRunner();
        handler = new WeakHandler(eventRunner);
        handler.postDelayed(runnable, NUM_TIME);

    }


    private void changeSelect(int pos) {
        switch (pos) {
            case 1:
                image1.setPixelMap(ResourceTable.Media_round_select);
                image2.setPixelMap(ResourceTable.Media_round_unsele);
                image3.setPixelMap(ResourceTable.Media_round_unsele);

                break;
            case 2:
                image1.setPixelMap(ResourceTable.Media_round_unsele);
                image2.setPixelMap(ResourceTable.Media_round_select);
                image3.setPixelMap(ResourceTable.Media_round_unsele);
                break;
            case NUM_3:
                image1.setPixelMap(ResourceTable.Media_round_unsele);
                image2.setPixelMap(ResourceTable.Media_round_unsele);
                image3.setPixelMap(ResourceTable.Media_round_select);
                break;
            default:
                break;
        }

        image1.invalidate();
        image2.invalidate();
        image3.invalidate();

    }

    private List<CenterBean> createcenterBean() {
        List<CenterBean> list = new ArrayList<>();
        list.add(new CenterBean(ResourceTable.Media_boardwalk_by_the_ocean_gaitubao2, 0));
        list.add(new CenterBean(ResourceTable.Media_journal_and_coffee_at_table2_gaitubao_2, 1));
        list.add(new CenterBean(ResourceTable.Media_tying_down_tent_fly_gaitubao_2, 2));
        return list;
    }

    private List<Image> createImages() {
        List<Image> pageList = new ArrayList<>();
        pageList.add(createPageImage(ResourceTable.Media_boardwalk_by_the_ocean));
        pageList.add(createPageImage(ResourceTable.Media_journal_and_coffee_at_table));
        pageList.add(createPageImage(ResourceTable.Media_tying_down_tent_fly));
        return pageList;
    }

    private Image createPageImage(int imageId) {
        Image image = new Image(this);
        image.setPixelMap(imageId);
        image.setWidth(DisplayManager.getInstance().getDefaultDisplay(this).get().getAttributes().width);

        return image;
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     * @param intent intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void debug(String msg) {
        HiLog.error(LOG_LABEL, msg);
    }
}
